package com.jiulan.hearthearing.ui.login.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.common.MethodUtils;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.login.model.LoginModel;
import com.jiulan.hearthearing.utils.SoftKeyboardUtils;
import com.jiulan.hearthearing.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 密码登录
 */
public class LoginPasswordActivity extends BaseActivity implements View.OnClickListener {
    private String phone = "", password = "";
    private boolean isPasswordVisiable = false;

    private EditText etLoginPhone;
    private EditText etLoginPassword;
    private ImageView ivClear;
    private ImageView ivEye;
    private TextView tvForgetPassword;
    private TextView tvLogin;
    private TextView goCodeLogin;
    private TextView goRegister;
    private ImageView ivWechatLogin;
    private ImageView ivQQLogin;
    private ImageView ivWeiboLogin;

    public static void actionStart(Context context, String phone) {
        Intent intent = new Intent(context, LoginPasswordActivity.class);
        intent.putExtra("phone", phone);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_login_password;
    }

    @Override
    protected void initView() {
        phone = getIntent().getStringExtra("phone");
        etLoginPhone = findViewById(R.id.etLoginPhone);
        etLoginPassword = findViewById(R.id.etLoginPassword);
        ivClear = findViewById(R.id.ivClear);
        ivEye = findViewById(R.id.ivEye);
        tvForgetPassword = findViewById(R.id.tvForgetPassword);
        tvLogin = findViewById(R.id.tvLogin);
        goCodeLogin = findViewById(R.id.goCodeLogin);
        goRegister = findViewById(R.id.goRegister);
        ivWechatLogin = findViewById(R.id.ivWechatLogin);
        ivQQLogin = findViewById(R.id.ivQQLogin);
        ivWeiboLogin = findViewById(R.id.ivWeiboLogin);
        ivClear.setOnClickListener(this);
        ivEye.setOnClickListener(this);
        tvForgetPassword.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        goCodeLogin.setOnClickListener(this);
        goRegister.setOnClickListener(this);
        ivWechatLogin.setOnClickListener(this);
        ivQQLogin.setOnClickListener(this);
        ivWeiboLogin.setOnClickListener(this);
        etLoginPhone.setText(phone);
        etLoginPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    phone = s.toString().trim();
                } catch (Exception e) {
                    phone = "";
                }
                checkInput();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etLoginPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    password = s.toString().trim();
                } catch (Exception e) {
                    password = "";
                }
                checkInput();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_DEFAULT;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivClear://一键删除手机号
                etLoginPhone.setText("");
                break;
            case R.id.ivEye://密码是否明显
                if (isPasswordVisiable) {//可见时，点击变为不可见
                    etLoginPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    ivEye.setImageResource(R.mipmap.eye_check);
                    isPasswordVisiable = false;
                } else {//不可见时，点击变为可见
                    etLoginPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    ivEye.setImageResource(R.mipmap.eye_close);
                    isPasswordVisiable = true;
                }
                break;
            case R.id.tvForgetPassword://忘记密码
                break;
            case R.id.goCodeLogin://go验证码登录界面
                LoginVerificationCodeActivity.actionStart(getContext(), phone);
                break;
            case R.id.goRegister://go注册界面
                RegisterActivity.actionStart(getContext());
                break;
            case R.id.tvLogin://登录
                SoftKeyboardUtils.hideSoftKeyboard(this);
                login();
                break;
            case R.id.ivWechatLogin://微信登录
                break;
            case R.id.ivQQLogin://QQ登录
                break;
            case R.id.ivWeiboLogin://微博登录
                break;
        }
    }

    private void checkInput() {
        if (StringUtils.isPhoneNumber(phone) && StringUtils.isPasswordCorrect(password)) {
            tvLogin.setClickable(true);
            tvLogin.setBackgroundResource(R.drawable.login_blue_bg);
        } else {
            tvLogin.setClickable(false);
            tvLogin.setBackgroundResource(R.drawable.login_gray_bg);
        }
    }

    /**
     * 登录
     */
    private void login() {
        Map<String, String> map = new HashMap<>();
        map.put("account", phone);
        map.put("password", password);
        map.put("spread", "0");
        new HttpRequest(getContext()).doPost(UrlConstants.login_password, "", map, LoginModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof LoginModel) {
                    LoginModel loginModel = (LoginModel) result;
                    MethodUtils.loginAfter(LoginPasswordActivity.this, phone, loginModel.getData().getToken());
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }
}
